﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace DunSilverlight.ViewModel
{
    public class UserModel : ModelBase<UserModel,DunLibrary.User.User>,DunLibrary.Core.IItem
    {
        private string password;
        public string Password
        {
            get
            {
                return this.password;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    throw new Exception("密码不能为空");
                }
                this.password = value;
                OnNotifyPropertyChanged("Password");
            }
        }

        private bool isRoleInit = false;
        private RoleModel role;
        public RoleModel Role
        {
            get
            {
                if (!isRoleInit && this.role == null)
                {
                    isRoleInit = true;
                    this.role = new RoleModel();
                }
                return this.role;
            }
            set
            {
                isRoleInit = true;
                this.role = value;
            }
        }

        private bool isOrganizationInit = false;
        private OrganizationModel organization;
        public OrganizationModel Organization
        {
            get
            {
                if (!isOrganizationInit && this.organization == null)
                {
                    isOrganizationInit = true;
                    this.organization = new OrganizationModel();
                }
                return this.organization;
            }
            set
            {
                isOrganizationInit = true;
                this.organization = value;
            }
        }

        public override DunLibrary.User.User ToEntity()
        {
            var obj = base.ToEntity();
            obj.Password = this.Password;
            if (this.Role.Id != 0)
                obj.Role = this.Role.ToEntity();
            if (this.Organization.Id != 0)
                obj.Organization = this.Organization.ToEntity();
            return obj;

        }

        /// <summary>
        /// 使用new隐藏父类的static函数
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public new static UserModel Create(DunLibrary.User.User entity)
        {
            if (entity == null)
                return null;
            UserModel obj = new UserModel();
            obj.Id = entity.Id;
            obj.Name = entity.Name;
            obj.Number = entity.Number;
            obj.GUID = entity.GUID;
            obj.IsUsed = entity.IsUsed;
            obj.Description = entity.Description;
            obj.Password = entity.Password;

            if (entity.Role != null)
                obj.role = RoleModel.Create(entity.Role);
            if (entity.Organization != null)
                obj.Organization = OrganizationModel.Create(entity.Organization);

            return obj;
        }


        public override bool Validation()
        {
            if (string.IsNullOrEmpty(Name) || string.IsNullOrEmpty(Number) || string.IsNullOrEmpty(Password)
                || Role.Id == 0 || Organization.Id==0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

     
    }
}
